<?php
	/*
	* Metaboxes that does not contain any image or upload fields
	*/

	add_action( 'admin_init', 'add_some_meta_box', 1 );

	function add_some_meta_box()
	{
		
		add_meta_box(
			'featured-project',
			'Featured Project',
			'render_featured_content',
			'dmci_projects',
			'normal',
			'high'
		);
		
		add_meta_box(
			'project-logo',
			'Project Logo',
			'render_project_logo',
			'dmci_projects',
			'side',
			'high'
		);
		
		add_meta_box(
			'project-slider',
			'Project Slider',
			'render_project_slider',
			'dmci_projects',
			'normal',
			'high'
		);
		
		add_meta_box(
			'page-slider',
			'Page Slider',
			'render_page_slider',
			'page',
			'normal',
			'high'
		);
		
				
		add_meta_box(
			'cons-update-datepicker',
			'Construction Update Date',
			'render_datepicker',
			'dmci_cons_update',
			'side',
			'low'
		);
		
		add_meta_box(
			'cons-slider',
			'Construction Update Slider',
			'render_cons_slider',
			'dmci_cons_update',
			'normal',
			'low'
		);
	}
	
	
	
	/*Datepicker*/
	function render_datepicker($post){
		wp_enqueue_script('jquery-ui-datepicker');  
    	wp_enqueue_style('jquery-ui-custom', get_template_directory_uri().'/css/jquery-ui-1.10.2.custom.css');
		
		
		
		$html = '';
		$cupdate_date = get_post_meta($post->ID, 'cons_update_date', true);
		if( empty($cupdate_date) ){
			$cupdate_date = date('m/d/Y');	
		}
		wp_nonce_field(plugin_basename(__FILE__), 'cons_update_date_nonce'); 

		$html .= '<label>Update Date: </label><input type="text" name="cons_update_date" class="datepicker" value="'.$cupdate_date.'" />';
		
		echo $html;
		
	}
	
	add_action('save_post', 'save_datepicker');
	
	function save_datepicker($post_id){
		
		global $post;
		
		if(!isset($_POST['cons_update_date_nonce']) || !wp_verify_nonce($_POST['cons_update_date_nonce'], plugin_basename(__FILE__))) { 
			return $post_id;
		} 
		
		if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {  
			return $post_id;
		}
		
		if('dmci_cons_update' != $_POST['post_type']) {  
		  if(!current_user_can('edit_page', $post_id)) {  
			return $post_id;  
		  } 
		} 
		else {  
			if(!current_user_can('edit_page', $post_id)) { 
				return $post_id;  
			} 
		}
		
		
		if( $post->post_type == "dmci_cons_update" ) {
			
			
			$the_date = !empty($_POST['cons_update_date']) ? esc_attr($_POST['cons_update_date']) : date('m/d/Y');
			update_post_meta($post->ID, 'cons_update_date', $the_date);
		}
		
	}
	/*Datepicker*/
	
	/* Project Feactured */
	function render_featured_content($post){
		
		
	
		$featured = get_post_meta($post->ID, 'featured-project', true);
		$yes = $featured == 'Yes' ? 'selected="selected"' : '';
		$no = $featured == 'No' ? 'selected="selected"' : '';
		echo 'Select yes below to this project featured and show in the front page <br/>';
		echo '<select name="is-featured">
        		<option value="No" ' .$no. '>No</option>
        		<option value="Yes" '.$yes.'>Yes</option>
    		</select>';
		
	}
	
	add_action('save_post', 'save_featured_project');
	
	function save_featured_project($post_ID){
		global $post;
			
		if( isset($post) && $post->post_type == "dmci_projects" ) {
		$result = update_post_meta($post_ID, 'featured-project', esc_attr($_POST['is-featured'])); 
		
		}
	}
	
	/* End Project Featured */